﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.Model.Project;

using Jzy.BLL.Project;
using Jzy.Core.Common.Web;

using System.Collections.Generic;


using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Dict;
using Frame.Lib.Enums;

public partial class Project_Edit : PageEdit
{
    private ProjectBO _prj = new ProjectBO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDrp();
            if (PKID != null)
            {
                DispInfo();               
            }
            else
            {
                Status.SelectedIndex = 0;
                Importance.SelectedIndex = 0;
                TypeCode.SelectedIndex = 0;

                RemoveStatusItem(PrjStatus.归档, PrjStatus.结束, PrjStatus.作废);               
            }
        }
        AddAttr();
    }

    private void AddAttr()
    {

        JSUtil.ShowDeptUserTreeDlg(PMID, PMName, true, null);
        JSUtil.ShowDeptUserTreeEx(InnerMemberIDs, InnerMembers);

        WebUtil.SetTimeBox(StartTime, false);
      

        WebUtil.SetTimeBox(EndTime, false);
     
    }

    private void DispInfo()
    {
        ProjectInfo info = _prj.GetInfo(PKID);
        EntityUtil.SetControl(pnlMain, info);
        PMID.Value = (-info.PMID).ToString();
        StartTime.Text = info.StartTime.ToString("yyyy-MM-dd");
        EndTime.Text = info.EndTime.ToString("yyyy-MM-dd");
        Budget.Text = info.Budget.ToString("N02");

        PrjStatus status = (PrjStatus)info.Status;
        switch (status)
        {
            case PrjStatus.启动:
                RemoveStatusItem(PrjStatus.归档);               
                break;
            case PrjStatus.结束:
                RemoveStatusItem(PrjStatus.暂停, PrjStatus.作废);
                break;
            case PrjStatus.暂停:
                RemoveStatusItem(PrjStatus.结束, PrjStatus.归档);
                break;
            case PrjStatus.作废:
                RemoveStatusItem(PrjStatus.归档, PrjStatus.结束, PrjStatus.暂停);
                break;
            case PrjStatus.归档:
                RemoveStatusItem(PrjStatus.结束, PrjStatus.暂停, PrjStatus.作废);
                break;
        }
    }

    private void RemoveStatusItem(params PrjStatus[] arrStatus)
    {
        for (int i = 0; i < Status.Items.Count; i++)
        {
            PrjStatus status = (PrjStatus)int.Parse(Status.Items[i].Value);
            if (arrStatus.Contains(status))
            {
                Status.Items.RemoveAt(i);
                i--;
            }
            
        }
    }

    private void BindDrp()
    {
        WebUtil.BindListControlWithEnum(Status,typeof(PrjStatus));

        WebUtil.BindListControlWithEnum(Importance, typeof(PrjImportance));

        DictBO dict = new DictBO();

        dict.BindListControl(TypeCode, (int)DictItem.项目类型);
       
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        ProjectInfo info = _prj.GetInfo(PKID);
        if (Budget.Text == "")
        {
            Budget.Text = "0";
        }
        EntityUtil.SetEntity(pnlMain, info);

        info.CreatorID = AppSession.UserID;
        info.CreatorName = AppSession.UserName;
        info.PMID = -info.PMID;
        

        _prj.Save(info);
        JSUtil.RefreshView();
    }
}
